package cn.linstudy.biz.client;

import cn.linstudy.biz.ExecutorBiz;
import cn.linstudy.biz.model.IdleBeatParam;
import cn.linstudy.biz.model.KillParam;
import cn.linstudy.biz.model.LogParam;
import cn.linstudy.biz.model.LogResult;
import cn.linstudy.biz.model.TriggerParam;
import cn.linstudy.vo.result.ResponseBean;
import cn.linstudy.utils.JobRemotingUtils;


/**
 * @Author XiaoLin
 * @Date 2022/1/8 13:29
 * @Description
 */
public class ExecutorBizClient implements ExecutorBiz {

    public ExecutorBizClient() {
    }
    public ExecutorBizClient(String addressUrl, String accessToken) {
        this.addressUrl = addressUrl;
        this.accessToken = accessToken;

        // valid
        if (!this.addressUrl.endsWith("/")) {
            this.addressUrl = this.addressUrl + "/";
        }
    }

    private String addressUrl ;
    private String accessToken;
    private int timeout = 3;


    @Override
    public ResponseBean<String> beat() {
        return JobRemotingUtils.postBody(addressUrl+"beat", accessToken, timeout, null, String.class);
    }

    @Override
    public ResponseBean<String> idleBeat(IdleBeatParam idleBeatParam){
        return JobRemotingUtils.postBody(addressUrl+"idleBeat", accessToken, timeout, idleBeatParam, String.class);
    }

    @Override
    public ResponseBean<String> run(TriggerParam triggerParam) {
        return JobRemotingUtils.postBody(addressUrl + "run", accessToken, timeout, triggerParam, String.class);
    }

    @Override
    public ResponseBean<String> kill(KillParam killParam) {
        return JobRemotingUtils.postBody(addressUrl + "kill", accessToken, timeout, killParam, String.class);
    }

    @Override
    public ResponseBean<LogResult> log(LogParam logParam) {
        return JobRemotingUtils
            .postBody(addressUrl + "log", accessToken, timeout, logParam, LogResult.class);
    }
}
